/*
 * @Author: cygao
 * @Date: 2018-11-09 13:58:25
 * @Last Modified by: caijw
 * @Last Modified time: 2019-01-05 16:07:46
 */
import React, { Component } from 'react';
import './community.less';
import { connect } from "react-redux";
import {loadMore} from '../../../utils';
import { Toast } from 'react-weui';
import {Link, withRouter} from 'react-router-dom';
import * as actions from '../../../store/action/community';
import Resume from './resume/resume'
import Fun from './fun/fun'
import FunFirst from './fun/funFirst'

class Community extends Component {
    constructor() {
        super()
        this.state = {
            tab: 0
        }
    }
    componentDidMount(){
        this.props.community.applyJob.list = [];
        this.props.community.list = [];
        this.props.community.page = 1;
        this.props.community.applyJob.page = 1;
        this.props.fetchTopicList();
        this.props.fetchApplyJobList();
        loadMore(this.mainContent,this.props.fetchTopicList);
        loadMore(this.mainContent,this.props.fetchApplyJobList);
    }
    changeTab(tab){
        this.setState({
            tab: tab
        })

        //TODO 这里先跳转到登录页面后面可能要优化
        if(tab != 0){
            //判断是否登陆
            this.isLogin();
        }
    }


    //退出登陆
    logout = () =>{
        localStorage.setItem('token', '');
        //判断是否登陆
        this.isLogin();
    }
    //判断是否登陆
    isLogin = () => {
        if(!localStorage.getItem('token')){
            this.props.history.push('/glogin')
        }
    }
    render() {
        if(this.state.tab === 0){
           
        }
        return (
            <div id="community-container" ref={ref=>this.mainContent=ref}>
                <Toast icon="loading" show={this.props.community.fetching}>Loading...</Toast>
                <div className="community-header">
                    <div className="tab">
                        <span onClick={ this.changeTab.bind(this, 0) } className={ this.state.tab === 0 ? 'active': ''}>趣事分享</span>
                        <span onClick={ this.changeTab.bind(this, 1) } className={ this.state.tab === 1 ? 'active': ''}>求职</span>
                    </div>
                    <Link to='edit'><div className="publish">
                        <i className="icon-add"></i>
                        <span>发布</span>
                    </div>
                    </Link>
                </div>
                <div className="list-container">
                { this.state.tab === 0 ? (
                    <FunFirst list={this.props.community.list} type="list"/>
                  ) : (
                    
                        this.props.community.applyJob.list.length>0?this.props.community.applyJob.list.map((item, index) => {
                            return <Resume data={item}/>
                        }) : <div></div>
                    
                )}
                </div>
            </div>
        )
    }
}

export default connect((state)=>{return {...state}}, actions)(Community);